package com.lucene.blog.controller;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lucene.blog.bean.Blog;
import com.lucene.blog.bean.Blogger;
import com.lucene.blog.bean.PageBean;
import com.lucene.blog.db.service.BlogService;
import com.lucene.blog.db.service.BloggerService;
import com.lucene.blog.util.PageUtil;
import com.lucene.blog.util.StringUtil;

/**
 * 主页controller
 * 
 * @author yachao
 *
 */
@Controller
@RequestMapping("/")
public class IndexController {

	@Resource
	private BloggerService bloggerService;	
	@Resource
	private BlogService blogService;

	/**
	 * 请求主页
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(@RequestParam(value = "page", required = false) String page,
							  @RequestParam(value = "blogTypeId", required = false) String blogTypeId,
							  @RequestParam(value = "releaseDateStr", required = false) String releaseDateStr,
							  HttpServletRequest request) throws Exception {
		ModelAndView modelAndView = new ModelAndView();

		Blogger blogger = null;
		try {
			blogger = bloggerService.getBloggerData();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		if (StringUtil.isEmpty(page)) {
			page = "1";
		}

		// 获取分页的bean
		PageBean pageBean = new PageBean(Integer.parseInt(page), 10); // 每页显示10条记录

		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("pageBegin", pageBean.getPageBegin());
		condition.put("pageSize", pageBean.getPageSize());
		condition.put("blogTypeId", blogTypeId);
		condition.put("releaseDateStr", releaseDateStr);

		List<Blog> blogList = blogService.getBlogListWithPaging(condition);
		for (int i = 0; i < blogList.size(); i++) {
			Blog blog = blogList.get(i);
			List<String> imageList = blog.getImageList();
			String blogInfo = blog.getContent();
			Document doc = Jsoup.parse(blogInfo);
			Elements jpgs = doc.select("img[src$=.jpg]");

			for (int j = 0; j < jpgs.size(); j++) {
				Element jpg = jpgs.get(j);
				imageList.add(jpg.toString());
				if (j == 2) {
					break; // 只存三张图片信息
				}
			}
		}

		// 分页
		StringBuffer param = new StringBuffer();
		// 拼接参数，主要对于点击文章分类或者日期分类后，查出来的分页，要拼接具体的参数
		if (StringUtil.isNotEmpty(blogTypeId)) {
			param.append("blogTypeId=" + blogTypeId + "&");
		}
		if (StringUtil.isNotEmpty(releaseDateStr)) {
			param.append("releaseDateStr=" + releaseDateStr + "&");
		}
		modelAndView.addObject("pageCode", PageUtil.getPagination(request.getContextPath() + "/index.html",
				blogService.getBlogCount(condition), Integer.parseInt(page), 10, param.toString()));
		
		modelAndView.addObject("blogger", blogger);
		modelAndView.addObject("blogList", blogList);
		modelAndView.addObject("commonPage", "front/blog/blogList.jsp");
		modelAndView.addObject("title", "博客首页");
		modelAndView.setViewName("mainTemp");
		
		return modelAndView;
	}

}
